//
//  BaseDetailViewController.m
//  RJETGeography
//
//  Created by RJET04 on 2023/8/23.
//

#import "BaseDetailViewController.h"

@interface BaseDetailViewController ()

@property (nonatomic, strong) ZYView *navView;
@property (nonatomic, strong) ZYImageView *backImgV;
@property (nonatomic, strong) ZYLabel *backLab;
@property (nonatomic, strong) ZYLabel *titleLab;
@property (nonatomic, assign) NSInteger num;

@end

@implementation BaseDetailViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupBgView];
    [self setupNaviViews];
    [self setupContentViews];
    [self configPlaceholder];
}

- (void)setupBgView
{
    ZYImageView *bgView = [ZYImageView createZYImageView:^(ZYImageView *imageView) {
        imageView.frameBlock(CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT))
        .imageBlock([UIImage imageNamed:@"bj"]);
    }];
    [self.view addSubview:bgView];
    [self.view sendSubviewToBack:bgView];
}

- (void)setupNaviViews
{
    self.navView = [ZYView createZYView:^(ZYView *view) {
        view.frameBlock(CGRectMake(0, 0, SCREEN_WIDTH, kStatusBarHeight+kNavBarHeight))
        .backGroundColorBlock(THEME_COLOE);
    }];
    [self.view addSubview:self.navView];
    
    self.backImgV = [ZYImageView createZYImageView:^(ZYImageView *imageView) {
        imageView.frameBlock(CGRectMake(10, kStatusBarHeight+kNavBarHeight/2-ARROW_HEIGHT/2, ARROW_WIDTH, ARROW_HEIGHT))
        .imageBlock([UIImage imageNamed:@"back"]);
    }];
    [self.navView addSubview:self.backImgV];
    
    self.backLab = [ZYLabel createZYLabel:^(ZYLabel *label) {
        label.frameBlock(CGRectMake(CGRectGetMaxX(self.backImgV.frame)+3, kStatusBarHeight, 40, kNavBarHeight))
        .backGroundColorBlock([UIColor clearColor])
        .fontBlock([UIFont systemFontOfSize:17])
        .textColorBlock([UIColor whiteColor])
        .textAlignmentBlock(NSTextAlignmentLeft);
    }];
    [self.navView addSubview:self.backLab];
    
    self.titleLab = [ZYLabel createZYLabel:^(ZYLabel *label) {
        label.frameBlock(CGRectMake(CGRectGetMaxX(self.backLab.frame), kStatusBarHeight, SCREEN_WIDTH-2*CGRectGetMaxX(self.backLab.frame), kNavBarHeight))
        .backGroundColorBlock([UIColor clearColor])
        .fontBlock([UIFont systemFontOfSize:18])
        .textColorBlock([UIColor whiteColor])
        .textAlignmentBlock(NSTextAlignmentCenter);
    }];
    [self.navView addSubview:self.titleLab];
    
    self.backLab.text = self.backText;
    self.titleLab.text = self.titleText;
    
    ZYView *backV = [ZYView createZYView:^(ZYView *view) {
        view.frameBlock(CGRectMake(0, kStatusBarHeight, CGRectGetMaxX(self.backLab.frame), kNavBarHeight))
        .backGroundColorBlock([UIColor clearColor])
        .userEnableBlock(YES);
    }];
    [self.navView addSubview:backV];
    
    __weak typeof(self) weakSelf = self;
    backV.singalTapBlock = ^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
}

- (void)calulateNumByText:(NSString *)lText
{
    if (lText.length) {
        self.num++;
    }
}

- (void)setupContentViews
{
    NSString *oneLText = [self.model.textjson.PageData[self.index] oneT];
    NSString *twoLText = [self.model.textjson.PageData[self.index] twoT];
    NSString *threeLText = [self.model.textjson.PageData[self.index] threeT];
    NSString *fourLText = [self.model.textjson.PageData[self.index] fourT];
    
    CGFloat LINEH = DETAIL_LAB_HEIGHT;
    CGFloat PADDING = 40;
    CGFloat topH = 60;
    
    [self calulateNumByText:oneLText];
    [self calulateNumByText:twoLText];
    [self calulateNumByText:threeLText];
    [self calulateNumByText:fourLText];
    
    
    for (NSInteger i=0; i<self.num; i++) {
        
        NSString *text;
        if(i == 0)text = oneLText;
        if(i == 1)text = twoLText;
        if(i == 2)text = threeLText;
        if(i == 3)text = fourLText;
        
        CGFloat H = [text getHeightWithWidth:DETAIL_LAB_WIDTH textColor:[UIColor blackColor] textFont:[UIFont systemFontOfSize:DELAUT_LAB_SIZE]];
        
        ZYLabel *l = [ZYLabel createZYLabel:^(ZYLabel *label) {
            label.frameBlock(CGRectMake(12, CGRectGetMaxY(self.navView.frame)+topH +(PADDING+LINEH)*i, DETAIL_LAB_WIDTH, H))
            .textBlock(text)
            .textColorBlock([UIColor blackColor])
            .fontBlock([UIFont systemFontOfSize:DELAUT_LAB_SIZE])
            .textAlignmentBlock(NSTextAlignmentLeft);
            
        }];
        [self.view addSubview:l];
        
        l.numberOfLines = 0;
        l.lineBreakMode = NSLineBreakByWordWrapping;
        
        ZYTextField *tf = [ZYTextField createZYTextField:^(ZYTextField *textField) {
            textField.frameBlock(CGRectMake(CGRectGetMaxX(l.frame)+20,
                                            CGRectGetMidY(l.frame)-DETAIL_LAB_HEIGHT/2,
                                            SCREEN_WIDTH-CGRectGetMaxX(l.frame)-40,
                                            DETAIL_LAB_HEIGHT))
            .placeholderBlock(self.model.textjson.currency.placeholder)
            .textColorBlock([UIColor blackColor])
            .fontBlock([UIFont systemFontOfSize:18]);
            
            textField.layer.cornerRadius = 5;
            textField.layer.masksToBounds = YES;
            textField.layer.borderWidth = 1;
            textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
            
            textField.keyboardType = UIKeyboardTypeDecimalPad;
        }];
        [self.view addSubview:tf];
        
        ZYView *lv = [ZYView createZYView:^(ZYView *view) {
            view.frameBlock(CGRectMake(0, 0, 15, DETAIL_LAB_HEIGHT))
            .backGroundColorBlock([UIColor clearColor]);
        }];
        tf.leftView = lv;
        tf.leftViewMode = UITextFieldViewModeAlways;

        
        if (i == 0) {
            self.oneL = l;
            self.oneT = tf;
        }else if (i == 1) {
            self.twoL = l;
            self.twoT = tf;
        }else if (i == 2) {
            self.threeL = l;
            self.threeT = tf;
        }else if (i == 3) {
            self.fourL = l;
            self.fourT = tf;
        }
    }
    
    self.oneL.text = oneLText;
    self.twoL.text = twoLText;
    self.threeL.text = threeLText;
    self.fourL.text = fourLText;
    
    CGFloat X = 50;
    self.submitLab = [ZYLabel createZYLabel:^(ZYLabel *label) {
        label.frameBlock(CGRectMake(X, CGRectGetMaxY(self.navView.frame)+PADDING+(PADDING+LINEH)*self.num + 100, SCREEN_WIDTH-2*X, 50))
        .backGroundColorBlock(BTN_BG_COLOE)
        .textColorBlock([UIColor whiteColor])
        .textAlignmentBlock(NSTextAlignmentCenter)
        .textBlock(self.model.textjson.currency.reckon)
        .userEnableBlock(YES);
        
        label.layer.cornerRadius = 10;
        label.layer.masksToBounds = YES;
    }];
    [self.view addSubview:self.submitLab];
    
    __weak typeof(self) weakSelf = self;
    self.submitLab.singalTapBlock = ^{
        
        if (![weakSelf judge]) {
            return;
        }
        
        RevealViewController *vc = [RevealViewController new];
        vc.model = weakSelf.model;
        vc.env = weakSelf.env;
        vc.backText = weakSelf.backText;
        vc.titleText = weakSelf.titleText;
        vc.res = [weakSelf calculate];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    
}

- (NSString *)calculate
{
    if (self.index == 0) {//PDandPN
        return [NSString stringWithFormat:@"%.0f°~90°", (90-[self.oneT.text floatValue])];
    }else if(self.index == 1){//scale
        return [NSString stringWithFormat:@"%.2f:%.2f", [self.oneT.text floatValue]/[self.oneT.text floatValue], [self.twoT.text floatValue]/[self.oneT.text floatValue]];
    }else if(self.index == 2){//DOP
        return [NSString stringWithFormat:@"%.2f", ([self.oneT.text floatValue]/[self.twoT.text floatValue])];
    }else if(self.index == 3){//PCLD
        return [NSString stringWithFormat:@"%.2f", ([self.oneT.text floatValue]/[self.twoT.text floatValue])];
    }else if(self.index == 4){//PGR
        return [NSString stringWithFormat:@"%.2f%%", ([self.oneT.text floatValue]-[self.twoT.text floatValue])/[self.twoT.text floatValue]*100];
    }else if(self.index == 5){//POP
        return [NSString stringWithFormat:@"%.2f%%", ([self.twoT.text floatValue]/[self.oneT.text floatValue])*100];
    }else if(self.index == 6){//PORP
        return [NSString stringWithFormat:@"%.2f%%", ([self.twoT.text floatValue]/[self.oneT.text floatValue])*100];
    }else if(self.index == 7){//BP
        return [NSString stringWithFormat:@"%.2f‰", ([self.twoT.text floatValue]/[self.oneT.text floatValue])*1000];
    }else if(self.index == 8){//NPGR
        return [NSString stringWithFormat:@"%.2f‰", ([self.twoT.text floatValue]/[self.oneT.text floatValue])*1000];
    }else if(self.index == 9){//precipitation
        return [NSString stringWithFormat:@"%.2fmm", ([self.twoT.text floatValue]+[self.oneT.text floatValue])];
    }else if(self.index == 10){//runoff
        return [NSString stringWithFormat:@"%.2fmm", ([self.oneT.text floatValue]-[self.twoT.text floatValue])];
    }else if(self.index == 11){//evaporation
        return [NSString stringWithFormat:@"%.2fmm", ([self.oneT.text floatValue]-[self.twoT.text floatValue])];
    }else if(self.index == 12){//sexratio
        return [NSString stringWithFormat:@"100:%.2f", ([self.oneT.text floatValue]/[self.twoT.text floatValue])*100];
    }else if(self.index == 13){//mci
        return [NSString stringWithFormat:@"%.2f", ([self.oneT.text floatValue]/[self.twoT.text floatValue])];
    }else if(self.index == 14){//ri
        return [NSString stringWithFormat:@"%.2f", ([self.oneT.text floatValue]/[self.twoT.text floatValue])];
    }else if(self.index == 15){//pad
        return [NSString stringWithFormat:@"%.2f", ([self.oneT.text floatValue]/[self.twoT.text floatValue])];
    }else if(self.index == 16){//pdop
        return [NSString stringWithFormat:@"%.2f", ([self.oneT.text floatValue]/[self.twoT.text floatValue])];
    }else if(self.index == 17){//poci
        return [NSString stringWithFormat:@"%.2f", ([self.oneT.text floatValue]/[self.twoT.text floatValue])];
    }else if(self.index == 18){//slope
        return [NSString stringWithFormat:@"%.2f", ([self.oneT.text floatValue]/[self.twoT.text floatValue])];
    }else if(self.index == 19){//daylength
        return [NSString stringWithFormat:@"%.2f", 2*(12-[self.oneT.text floatValue])];
    }else if(self.index == 20){//nightlength
        return [NSString stringWithFormat:@"%.2f", 2*(24-[self.oneT.text floatValue])];
    }else if(self.index == 21){//solarheight
        CGFloat num = [self.oneT.text floatValue] - [self.twoT.text floatValue];
        if (num < 0) {
            return [NSString stringWithFormat:@"%.2f°", 90-(-num)];
        }
        return [NSString stringWithFormat:@"%.2f°", 90-(num)];
    }else if(self.index == 22){//pbc
        return [NSString stringWithFormat:@"%.2f", ([self.twoT.text floatValue]/[self.oneT.text floatValue])];
    }else if(self.index == 23){//sunrise
        CGFloat t = (24-[self.oneT.text floatValue])/2;
        NSString *originStr = [NSString stringWithFormat:@"%.2f", t];
        NSRange range = [originStr rangeOfString:@"."];
        NSString *dotAheadStr = [originStr substringToIndex:range.location];
        NSString *dotAfterStr = [originStr substringFromIndex:range.location];
        NSInteger hour = [dotAheadStr integerValue];
        NSInteger minutes = [dotAfterStr floatValue] *60;
        return [NSString stringWithFormat:@"%ld:%ld", hour, minutes];
    }else if(self.index == 24){//sunset
        CGFloat t = (24-[self.oneT.text floatValue])/2+[self.oneT.text floatValue];
        NSString *originStr = [NSString stringWithFormat:@"%.2f", t];
        NSRange range = [originStr rangeOfString:@"."];
        NSString *dotAheadStr = [originStr substringToIndex:range.location];
        NSString *dotAfterStr = [originStr substringFromIndex:range.location];
        NSInteger hour = [dotAheadStr integerValue];
        NSInteger minutes = [dotAfterStr floatValue] *60;
        return [NSString stringWithFormat:@"%ld:%ld", hour, minutes];
//        return [NSString stringWithFormat:@"%.2f", (24-[self.oneT.text floatValue])/2+[self.oneT.text floatValue]];
    }else if(self.index == 25){//cdr
        return [NSString stringWithFormat:@"%.2f‰", ([self.twoT.text floatValue]/[self.oneT.text floatValue])*1000];
    }else if(self.index == 26){//rh
        if ([self.oneT.text floatValue] > [self.twoT.text floatValue]) {
            return [NSString stringWithFormat:@"%.2f", ([self.oneT.text floatValue]-[self.twoT.text floatValue])];
        }
        return [NSString stringWithFormat:@"%.2f", ([self.twoT.text floatValue]-[self.oneT.text floatValue])];
    }else if(self.index == 27){//sea
        if ([self.twoT.text floatValue] > [self.threeL.text floatValue]) {
            return [NSString stringWithFormat:@"%.2f°", [self.oneT.text floatValue] + ([ self.twoT.text floatValue] - [self.threeT.text floatValue])];
        }
        return [NSString stringWithFormat:@"%.2f°", [self.oneT.text floatValue] - ([ self.threeL.text floatValue] - [self.twoL.text floatValue])];
    }else if(self.index == 28){//timezone
        if ([self.oneT.text integerValue]%15 > 7.5) {
            return [NSString stringWithFormat:@"%ld", [self.oneT.text integerValue]/15+1];
        }else{
            return [NSString stringWithFormat:@"%ld", [self.oneT.text integerValue]/15];
        }
    }else if(self.index == 29){//localtime
        if ([self.threeT.text integerValue] > [self.fourT.text integerValue]) {
            NSInteger kk = 4*([self.threeT.text integerValue]-[self.fourT.text integerValue]);
            NSInteger m = [self.twoT.text integerValue]-kk;
            if (m == 0) {
                return [NSString stringWithFormat:@"%ld:00", [self.oneT.text integerValue]];
            }else if (m < 0){
                NSInteger num = (-m)/60;
                NSInteger yu = (NSInteger)(-m)%60;
                if (yu == 0) {
                    return [NSString stringWithFormat:@"%ld:00", [self.oneT.text integerValue]-num];

                }else{
                    NSInteger ys = 60-yu;
                    if (ys < 10) {
                        NSString *yus = [NSString stringWithFormat:@"0%ld",ys];
                        return [NSString stringWithFormat:@"%ld:%@", ([self.oneT.text integerValue]-(num+1)), yus];
                    }else{
                        NSString *yus = [NSString stringWithFormat:@"%ld",ys];
                        return [NSString stringWithFormat:@"%ld:%@", ([self.oneT.text integerValue]-(num+1)), yus];
                    }
                }
            }else{
                return [NSString stringWithFormat:@"%ld:%ld", [self.oneT.text integerValue], m];
            }
        }else if ([self.threeT.text floatValue] == [self.fourT.text floatValue]){
            
            return [NSString stringWithFormat:@"%.2f:%.2f", [self.oneT.text floatValue], [self.twoT.text floatValue]];
        }else{
            NSInteger kk = 4*([self.fourT.text floatValue]-[self.threeT.text floatValue]);
            NSInteger m = [self.twoT.text intValue]+kk;
            if (m == 0) {
                return [NSString stringWithFormat:@"%.2f:00", [self.oneT.text floatValue]];
            }else {
                NSInteger num = (m)/60;
                NSInteger yu = (NSInteger)(m)%60;
                if (yu == 0) {
                    return [NSString stringWithFormat:@"%ld:00", [self.oneT.text intValue]+num];
                }else{
                    NSInteger hh = [self.twoT.text integerValue]+yu;
                    if (hh > 60) {
                        NSInteger nums = hh/60;
                        NSInteger yushu = hh%60;
                        if (yushu < 10) {
                            NSString *yushuj = [NSString stringWithFormat:@"0%ld", yushu];
                            return [NSString stringWithFormat:@"%ld:%@", [self.oneT.text integerValue]+num+nums, yushuj];
                        }else{
                            return [NSString stringWithFormat:@"%ld:%ld", [self.oneT.text integerValue]+num+nums, yushu];
                        }
                    }else{
                        return [NSString stringWithFormat:@"%ld:%ld", [self.oneT.text integerValue]+num, [self.twoT.text integerValue]+yu];
                    }
                    
                }
            }
        }
    }else if(self.index == 30){//HarmonicPeriod
        return [NSString stringWithFormat:@"%.2f", (2*M_PI)/[self.oneT.text floatValue]];
    }else if(self.index == 31){//HarmonicFrequency
        return [NSString stringWithFormat:@"%.2f", (2*M_PI)/[self.oneT.text floatValue]];
    }else if(self.index == 32){//TSF
        CGFloat a = ([self.oneT.text floatValue]/[self.threeT.text floatValue]);
        CGFloat b = [self.twoT.text floatValue];
        CGFloat c = [self.fourT.text floatValue]-[self.threeT.text floatValue]-1;
        return [NSString stringWithFormat:@"%.2f", a/(b/c)];
    }else if(self.index == 33){//DD
        return [NSString stringWithFormat:@"%.2f", [self.oneT.text floatValue]/[self.twoT.text floatValue]];
    }else if(self.index == 34){//freedom
        return [NSString stringWithFormat:@"%.2f", [self.oneT.text floatValue]-2];
    }else if(self.index == 35){//chromaC
        return [NSString stringWithFormat:@"%.2f", ([self.oneT.text floatValue]*[self.oneT.text floatValue])/[self.threeT.text floatValue]];
    }else if(self.index == 36){//chromaM
        return [NSString stringWithFormat:@"%.2f", ([self.threeT.text floatValue]*[self.twoT.text floatValue])/[self.oneT.text floatValue]];
    }else if(self.index == 37){//chromaE
        return [NSString stringWithFormat:@"%.2f", ([self.threeT.text floatValue]*[self.oneT.text floatValue])/[self.twoT.text floatValue]];
    }else if(self.index == 38){//Emissions
        return [NSString stringWithFormat:@"%.2f", ([self.twoT.text floatValue]*[self.oneT.text floatValue])/[self.threeT.text floatValue]];
    }else if(self.index == 39){//PollutionE
        return [NSString stringWithFormat:@"%.2f", ([self.oneT.text floatValue])/[self.twoT.text floatValue]];
    }
    
    
    return @"";
}

- (void)configPlaceholder
{
    if (self.index == 1) {
        //比例尺
        self.oneT.placeholder = [NSString stringWithFormat:@"%@(m)", self.model.textjson.currency.placeholder];
        self.twoT.placeholder = [NSString stringWithFormat:@"%@(m)", self.model.textjson.currency.placeholder];

    }else if (self.index == 2) {
        //人口密度
//        self.oneT.placeholder = [NSString stringWithFormat:@"%@(m)", self.model.textjson.currency.placeholder];
        self.twoT.placeholder = [NSString stringWithFormat:@"%@(m²)", self.model.textjson.currency.placeholder];

    }else if (self.index == 3) {
        //人口耕地密度
        self.twoT.placeholder = [NSString stringWithFormat:@"%@(KM²)", self.model.textjson.currency.placeholder];

    }else if (self.index == 9) {
        //降水量计算
        self.oneT.placeholder = [NSString stringWithFormat:@"%@(mm)", self.model.textjson.currency.placeholder];
        self.twoT.placeholder = [NSString stringWithFormat:@"%@(mm)", self.model.textjson.currency.placeholder];

    }else if (self.index == 10) {
        //径流量计算
        self.oneT.placeholder = [NSString stringWithFormat:@"%@(mm)", self.model.textjson.currency.placeholder];
        self.twoT.placeholder = [NSString stringWithFormat:@"%@(mm)", self.model.textjson.currency.placeholder];

    }else if (self.index == 11) {
        //蒸发量计算
        self.oneT.placeholder = [NSString stringWithFormat:@"%@(mm)", self.model.textjson.currency.placeholder];
        self.twoT.placeholder = [NSString stringWithFormat:@"%@(mm)", self.model.textjson.currency.placeholder];

    }else if (self.index == 15) {
        //人口密度计算
        self.twoT.placeholder = [NSString stringWithFormat:@"%@(KM²)", self.model.textjson.currency.placeholder];

    }else if (self.index == 16) {
        //人口生理密度计算
        self.twoT.placeholder = [NSString stringWithFormat:@"%@(KM²)", self.model.textjson.currency.placeholder];

    }
}

- (BOOL)judge
{
    if (self.num == 1) {
        if (self.oneT.text.length == 0) {
            [self show:[NSString stringWithFormat:@"%@ %@", self.model.textjson.currency.placeholder,
                        self.oneL.text]];
            return NO;
        }
    }else if(self.num == 2){
        if (self.oneT.text.length == 0) {
            [self show:[NSString stringWithFormat:@"%@ %@", self.model.textjson.currency.placeholder,
                        self.oneL.text]];
            return NO;
        }
        if (self.twoT.text.length == 0) {
            [self show:[NSString stringWithFormat:@"%@ %@", self.model.textjson.currency.placeholder,
                        self.twoL.text]];
            return NO;
        }
    }else if(self.num == 3){
        if (self.oneT.text.length == 0) {
            [self show:[NSString stringWithFormat:@"%@ %@", self.model.textjson.currency.placeholder,
                        self.oneL.text]];
            return NO;
        }
        if (self.twoT.text.length == 0) {
            [self show:[NSString stringWithFormat:@"%@ %@", self.model.textjson.currency.placeholder,
                        self.twoL.text]];
            return NO;
        }
        if (self.threeT.text.length == 0) {
            [self show:[NSString stringWithFormat:@"%@ %@", self.model.textjson.currency.placeholder,
                        self.threeL.text]];
            return NO;
        }
    }else if(self.num == 4){
        if (self.oneT.text.length == 0) {
            [self show:[NSString stringWithFormat:@"%@ %@", self.model.textjson.currency.placeholder,
                        self.oneL.text]];
            return NO;
        }
        if (self.twoT.text.length == 0) {
            [self show:[NSString stringWithFormat:@"%@ %@", self.model.textjson.currency.placeholder,
                        self.twoL.text]];
            return NO;
        }
        if (self.threeT.text.length == 0) {
            [self show:[NSString stringWithFormat:@"%@ %@", self.model.textjson.currency.placeholder,
                        self.threeL.text]];
            return NO;
        }
        if (self.fourT.text.length == 0) {
            [self show:[NSString stringWithFormat:@"%@ %@", self.model.textjson.currency.placeholder,
                        self.fourL.text]];
            return NO;
        }
     
    }

    return YES;
}

- (void)show:(NSString *)alertMsg
{
    [AlertUtils showAlertMsg:alertMsg sureAction:^{
        
    } cancelAction:^{
        
    } vc:self env: self.env];
}

@end
